package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.GoodsMessage;
import com.songlanyun.modules.message.model.LogisticsMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.entity.OrderPackageExpress;
import com.songlanyun.modules.ship.service.OrderPackageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;


/**
 * 物流   --  物流消息
 */
@Service("logisticsInteractService")
public class LogisticsInteractServiceImpl implements IInteractMsgService {

    @Resource
    private OrderPackageService orderPackageService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        OrderPackage orderPackage = orderPackageService.getById(to.getModelId());
        //物流
        Order order = orderPackage.loadOrder();
        List<ChildOrder> childOrderList = order.getChildOrderList(true);
        String goodsTitle = childOrderList.get(0).getGoodsTitle();

        OrderConstant.LogisticsStatus logisticsStatus = OrderConstant.LogisticsStatus.SHIPPED;//物流状态
        switch (orderPackage.getGoodsPackageStatus()) {
            case WAIT_RECEIPT://已发货
                logisticsStatus = OrderConstant.LogisticsStatus.SHIPPED;
                break;
            case RECEIPTED://已收货
                logisticsStatus = OrderConstant.LogisticsStatus.RECEIVED;
                break;
        }

        String remark = "";
        if (OrderConstant.GoodsPackageStatus.WAIT_RECEIPT == orderPackage.getGoodsPackageStatus()
                && OrderConstant.GoodsPackageType.PICKUP == orderPackage.getGoodsPackageType()) {
            remark = String.format("你购买的%s等%d件商品已备货成功，请及时到店提取", goodsTitle,
                    order.getGoodsNumber());
        } else {
            remark = String.format("你购买的%s等%d件商品%s，点击查看物流", goodsTitle,
                    order.getGoodsNumber(), logisticsStatus.getMsg());
        }

        List<GoodsMessage> goodsList = new ArrayList<>();
        //增加子订单中的商品
        childOrderList.forEach(c -> {
            GoodsMessage goodsMessage = new GoodsMessage();
            goodsMessage.setGoodsId(c.getGoodsId());
            goodsMessage.setGoodsName(c.getGoodsTitle());
            goodsMessage.setGoodsImg(c.getGoodsImg());
            goodsMessage.setGoodsSpec(c.getSkuSetName());
            goodsList.add(goodsMessage);
        });

        //发送mq消息
        OrderPackageExpress express = orderPackage.loadExpress();
        String logisticCode = "";
        if (express != null) {
            logisticCode = express.getLogisticCode();
        }
        LogisticsMessage logisticsMessage = new LogisticsMessage(logisticsStatus, order.getId(), orderPackage.getId(),
                logisticCode, order.getExchange(), order.getGoodsNumber(), order.getTotalAmount(), order.getTotalPoint(),
                goodsList);

        PrivateMessageTO privateMessageTO = new PrivateMessageTO(order.getUserId(), PrivateMessageConstant.MessageType.LOGISTICS_MESSAGE, logisticsMessage, remark);
        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);

        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(order.getUserId())
                        .title(PrivateMessageConstant.MessageType.LOGISTICS_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );

    }
}
